package com.yourwork.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yourwork.model.StaffManagement;
import com.yourwork.model.TypeManagement;
import com.yourwork.service.StaffManagementServic;
import com.yourwork.service.TypeManagementService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


@Controller
@RequestMapping(value="/staffManagement")
//员工管理
public class StaffManagementController {
	@Autowired
	@Qualifier(value="employeeManageServic")
	private StaffManagementServic employeeManageServic;
	@Autowired
	@Qualifier(value="employeeTypeService")
	private TypeManagementService employeeTypeService;
	
	//查询
	@RequestMapping(value="/select",method={RequestMethod.GET})
	public String toEmployeeManage(Model m,@RequestParam(name="page",required=false,defaultValue="0")int page,
			@RequestParam(name="pageFalse",required=false,defaultValue="0")int pageFalse,
			@RequestParam(name="size",required=false,defaultValue="10") int size,
			HttpSession session){
		int count = employeeManageServic.countEmployee();
		if(page<0){page = 0;}
		if(page<=(count/size)&&count%size!=0){page=page+0;}
			else{if(count%size==0){page=count/size-1;}
			else{page = count/size;}}
		//查询在职员工
		try{List<StaffManagement> listOn = employeeManageServic.queryEmployeeManage(page,size,"在职");
		//查询离职员工
		List<StaffManagement> listOff = employeeManageServic.queryEmployeeManage(pageFalse,size,"离职");
		//查询记录总数
		
		int maxPageOn = employeeManageServic.queryMaxPage(page,size,"在职");
		int maxPageOff = employeeManageServic.queryMaxPage(page,size,"离职");
		m.addAttribute("maxPageOn", maxPageOn);
		m.addAttribute("maxPageOff", maxPageOff);
		m.addAttribute("pageFalse", pageFalse);//Attribute 存入page 与页面数据交互
		m.addAttribute("page", page);//Attribute 存入page 与页面数据交互
		m.addAttribute("size", size);//Attribute 存入size 与页面数据交互
		addAttribute(m, "employeecount",count, true);//Attribute 存入count(员工总个数) 与页面数据交互
		addAttribute(m, "employeeListOn", listOn, true);//Attribute 存入listOn（在职的员工） 与页面数据交互
		addAttribute(m, "employeeListOff", listOff, true);//Attribute 存入listOn（离职的员工） 与页面数据交互
		m.addAttribute("remind", session.getAttribute("remind"));//获取session中的提示信息
		session.setAttribute("remind", "");//清空session中的remind的数据
		m.addAttribute("managerNumber", session.getAttribute("managerNumber"));
		return "/work/employeeManage";
		}catch (Exception e) {
			// TODO: handle exception
			return "/work/employeeManage";
		}
	}
	//详情查询
	@RequestMapping(value="/detailedStaffManagement")
	public String detailedInformation(@RequestParam(name="id")int id,Model m){
		List<StaffManagement> listDetailed = new ArrayList<StaffManagement>();
		//id查询员工信息
		listDetailed = employeeManageServic.queryDetailed(id);
		//Attribute 存入listDetailed（对应id员工的详细信息） 与页面数据交互
		addAttribute(m, "detailedList",listDetailed, true);
		return "/work/employeeDetails";
	}
	//修改资料
	@RequestMapping(value="/modifyStaffManagement",method={RequestMethod.GET})
	public String updateEmployeeManage(
			@ModelAttribute(name="employeeManage") StaffManagement em,Model m){
		String remind = employeeManageServic.updateEmployee(em);
		m.addAttribute("remind", remind);
		return "redirect:/staffManagement/select";
	}
	//添加
	@RequestMapping(value="/addStaffManagement",method={RequestMethod.POST})
	public String insertEmployeeManage(@RequestParam(name="file") MultipartFile file,
			@ModelAttribute(name="employeeManage") StaffManagement em,Model m,HttpSession session){
		String photoURL = employeeManageServic.uploadFile(file);//储存图片返回图片地址
		em.setPhoto(photoURL);//将图片的存入em对象
		String remind = employeeManageServic.insertEmployee(em);//添加返回是否添加成功
		session.setAttribute("remind", remind);//存入提示信息
		return "redirect:/staffManagement/select";
	}
	//修改详情
	@RequestMapping(value="/modifyEmployeeDetails",method={RequestMethod.GET})
	public String toEmployeeDryails(@RequestParam(name="id") int id,Model m){
		List<StaffManagement> listem = employeeManageServic.queryDetailed(id);
		List<TypeManagement> listet = employeeTypeService.queryEmployeeType(1);
		m.addAttribute("listem", listem);
		m.addAttribute("listet", listet);
		return "/work/employeeDetails";
	}
	//添加详情
	@RequestMapping(value="/addEmployeeDetails",method={RequestMethod.GET})
	public String doEmployeeDryails(Model m){
		StaffManagement employeeManage = new StaffManagement();
		TypeManagement employeeType = new TypeManagement();
		List<StaffManagement> listem = new ArrayList<StaffManagement>();
		employeeManage.setEmployeeType(employeeType);
		listem.add(employeeManage);
		List<TypeManagement> listet = employeeTypeService.queryEmployeeType(1);
		m.addAttribute("listem", listem);
		m.addAttribute("listet", listet);
		return "work/employeeDetails2";
	}
	private void addAttribute(Model m,String attributeName,Object attributeValue,boolean replace){
		if(replace==true){//判断是否添加attribute
			m.addAttribute(attributeName, attributeValue);
		}else{
			if(!m.containsAttribute(attributeName)){//判断是否已经存在的attribute
				m.addAttribute(attributeName, attributeValue);
			}
		}
	}
}